package priv.jesse.mall.web.user;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import priv.jesse.mall.entity.Product;
import priv.jesse.mall.service.ProductService;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Controller
public class IndexController {
    @Autowired
    private ProductService productService;
    /**
     * 打开首页
     * @return
     */
    @RequestMapping("/index.html")
    public String toIndex() {
        return "mall/index";
    }

    /**
     * 访问根目录转发到首页
     * @return
     */
    @RequestMapping("/")
    public String index(){
        return "forward:/index.html";
    }


    /**
     * 打开首页
     * @return
     */
    @RequestMapping("/mallindex.html")
    public String toMallIndex(Map<String, Object> map,HttpServletRequest request) {
        List<Product> list = productService.findHotProduct();

        String urlprefix = request.getScheme() + "://"
                + request.getServerName() + ":"
                + request.getServerPort();

        for(int i=0;i<list.size();i++){
            String[] ss=list.get(i).getImage().split("&");
            String image=urlprefix.trim()+"/mall/admin/product/getImg.do?url="+ ss[0].replace("/","%2F").replace("\\","%2F");
            list.get(i).setImage(image);
        }
        map.put("product", list);
        return "newmall/index";
    }



    /**
     * 打开分类
     * @return
     */
    @RequestMapping("/category.html")
    public String toCategory() {
        return "newmall/category";
    }




    /**
     * 打开我的页面
     * @return
     */
    @RequestMapping("/userhome.html")
    public String toUserhome() {
        return "newmall/userhome";
    }





}
